package com.bw.widget.header

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.ConvertUtils
import com.bw.widget.R
import com.scwang.smart.refresh.layout.api.RefreshHeader
import com.scwang.smart.refresh.layout.api.RefreshKernel
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.scwang.smart.refresh.layout.constant.SpinnerStyle


class MDMRefreshHeader(context: Context, attrs: AttributeSet?) : LinearLayout(context,attrs),RefreshHeader {

    private var root : View
    private var tv : TextView

    init {
        root = LayoutInflater.from(context).inflate(R.layout.layout_mdm_header,this,false)
        val layoutParams = LayoutParams(
            LayoutParams.MATCH_PARENT, ConvertUtils.dp2px(69f))
        root.layoutParams = layoutParams
        tv = root.findViewById(R.id.mdm_head_title)
        addView(root)
    }

    @SuppressLint("RestrictedApi")
    override fun onStateChanged(
        refreshLayout: RefreshLayout,
        oldState: RefreshState,
        newState: RefreshState
    ) {
        if(newState == RefreshState.None)
             tv.text = "下拉刷新"
        else if (newState == RefreshState.Refreshing)
            tv.text = "正在刷新"
        else
            tv.text = "释放刷新"
//        when (newState) {
//            RefreshState.None, RefreshState.PullDownToRefresh -> tv.text = "下拉刷新"
//            RefreshState.Refreshing -> tv.text = "正在刷新"
//            RefreshState.ReleaseToRefresh -> tv.text = "释放刷新"
//        }
    }

    override fun getView(): View {
        return this
    }

    override fun getSpinnerStyle(): SpinnerStyle {
        return SpinnerStyle.Translate;
    }

    @SuppressLint("RestrictedApi")
    override fun setPrimaryColors(vararg colors: Int) {
    }

    @SuppressLint("RestrictedApi")
    override fun onInitialized(kernel: RefreshKernel, height: Int, maxDragHeight: Int) {
    }

    @SuppressLint("RestrictedApi")
    override fun onMoving(
        isDragging: Boolean,
        percent: Float,
        offset: Int,
        height: Int,
        maxDragHeight: Int
    ) {
    }

    @SuppressLint("RestrictedApi")
    override fun onReleased(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
    }

    @SuppressLint("RestrictedApi")
    override fun onStartAnimator(refreshLayout: RefreshLayout, height: Int, maxDragHeight: Int) {
    }

    @SuppressLint("RestrictedApi")
    override fun onFinish(refreshLayout: RefreshLayout, success: Boolean): Int {
        return 0
    }

    @SuppressLint("RestrictedApi")
    override fun onHorizontalDrag(percentX: Float, offsetX: Int, offsetMax: Int) {
    }

    override fun isSupportHorizontalDrag(): Boolean {
        return false
    }
}